package test.adddata;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import utils.SmallTool;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: gcr
 * @Date: 2022-3-30 15:46
 */
public class 检查本地xml与本地pm表pmid是不是一致256G {

    public static String localPm = "pubmed_vip_extend_0328_33771437";
    static String dbName1 = "xml_content_3qw";
    static String dbName2 = "xml_content_1_3qw";
    // 全部插入不判断 如果是true 则进行全部插入
    static boolean insert = false;
    //是不是要更新

    static boolean isupdate = false;
    //通过id进行开始

    // 2022年5月25日08:41:02 直接查询 防止表损坏


    @SneakyThrows
    public static void main(String[] args) {
        //  2021年11月18日15:41:15 插入2800万以后的数据,进行查询 没有的才插入,有就先不更新了
        //先删除 后插入

//        DSFactory.setCurrentDSFactory(new DruidDSFactory());
//        DataSource ds = DSFactory.get("server_local");
//        DataSource taiyids = DSFactory.get("taiyi");
        Set<String> collect = Stream.of(dbName1, dbName2).collect(Collectors.toSet());
        for (String s : collect) {
            int startPmid = 0;
            Db use = Db.use();
            String allDate = "select max(pmid) as pmid  from " + s;
            Entity query = use.query(allDate).get(0);
            SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

            Integer count = query.getInt("pmid");
            int breakSize = 100000;
            int pageSize = count / breakSize + 1;
            SmallTool.printMess("全部页码+++++++++" + pageSize);
            SmallTool.printMess("检查开始");

            SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
            for (int i = 1; i <= pageSize; i++) {
                SmallTool.printMemory();
                SmallTool.printMess("当前页码+++++" + i);
                int start = breakSize * (i - 1) + 1;
                int end = i * breakSize;
                SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
                if (end <= startPmid) {
                    SmallTool.printMess("因为小于" + startPmid + " 略过了");
                    continue;
                }
                String paramSql = "select pmid from " + s + " where  pmid between  " + start + " and " + end;
                String pmdb = "select pmid from " + localPm + " where  pmid between  " + start + " and " + end;
                List<Entity> querysxml = use.query(paramSql);
                if (querysxml.size() == 0) {
                    continue;
                }
                List<Entity> pmdbs = use.query(pmdb);
                Set<Integer> mexml = querysxml.stream().map(o -> o.getInt("pmid")).collect(Collectors.toSet());
                Set<Integer> mepmdb = pmdbs.stream().map(o -> o.getInt("pmid")).collect(Collectors.toSet());
                if (mexml.size() != mepmdb.size()) {
                    SmallTool.printMessError("数量不一致++start++" + start + "++end++" + end);
                    if (mexml.size() > mepmdb.size()) {
                        mexml.removeAll(mepmdb);
                        FileUtil.appendString(JSON.toJSONString(mexml) + "\n", "/usr/local/chayi.log", "UTF-8");
                    } else {
                        mepmdb.removeAll(mexml);
                        FileUtil.appendString(JSON.toJSONString(mepmdb) + "\n", "/usr/local/chayi.log", "UTF-8");
                    }
                }

            }
        }


    }


}
